package com.csdj.controller;

import com.csdj.pojo.EnterpriseTalent;
import com.csdj.service.EnterpriseTalentService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class EnterpriseTalentController {
    @Autowired
    private EnterpriseTalentService  transientService;

    @RequestMapping(value ="/getEnterpriseTalent" )
    public String getEnterpriseTalent(HttpSession session,Integer id){
        List<EnterpriseTalent> talents = transientService.selectAll(id);
        session.setAttribute("talents",talents);
        session.setAttribute("id",id);
        return "main";
    }

    @RequestMapping(value ="/addEnterpriseTalent")
    public String addEnterpriseTalent(@ModelAttribute("enterpriseTalent") EnterpriseTalent enterpriseTalent){
        int row=transientService.addEnterpriseTalent(enterpriseTalent);
        if(row>0){
            return "redirect:/getEnterpriseTalent?id=0";
        }else {
            return "addInfo";
        }
    }
    @RequestMapping("/deleteEnterpriseTalent/{id}")
    public String deleteEnterpriseTalent(@PathVariable Integer id){
        boolean result =transientService.removeById(id);
        return "redirect:/getEnterpriseTalent?id=0";
    }

    @RequestMapping("/getEnterpriseTalentByUpdate/{id}")
    public String getEnterpriseTalentByUpdate(HttpSession session,@PathVariable Integer id){
        List<EnterpriseTalent> talents=(List<EnterpriseTalent>)session.getAttribute("talents");
        for(EnterpriseTalent talent:talents){
            if(talent.getId()==id){
                session.setAttribute("talent",talent);
                break;
            }
        }
        return "update";
    }

    @RequestMapping("/updateEnterpriseTalent")
    public String updateEnterpriseTalent(@ModelAttribute("enterpriseTalent") EnterpriseTalent enterpriseTalent){
        int row=transientService.updateEnterpriseTalent(enterpriseTalent);
        if(row>0){
            return "redirect:/getEnterpriseTalent?id=0";
        }else {
            return "update";
        }
    }
}
